<?php

class SummaryController extends Controller{

    private $M_summarys;
    private $M_sources;

    /**
     * 构造方法
     * 初始化
     */
    public function __construct(){
        $this->M_summarys = Model::getInstance('summarys');
        $this->M_sources = Model::getInstance('sources');
    }
    //模块首页
    public function index(){
        global $controller;
        global $config_arr;

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);
        $title = $this->input()->post('title');
        $status = $this->input()->post('status');
        $source_id = $this->input()->post('source_id');
        $p_stime = $this->input()->post('p_stime');
        $p_etime = $this->input()->post('p_etime');
        $u_stime = $this->input()->post('u_stime');
        $u_etime = $this->input()->post('u_etime');
        $filter = array();

        if($title){
            $filter['title'] = $title;
        }

        if($status){
            $filter['status'] = $status;
        }

        if($source_id){
            $filter['source_id'] = $source_id;
        }

        if($p_stime){
            $filter['p_stime'] = $p_stime;
        }

        if($p_etime){
            $filter['p_etime'] = $p_etime;
        }

        if($u_stime){
            $filter['u_stime'] = $u_stime;
        }

        if($u_etime){
            $filter['u_etime'] = $u_etime;
        }

        $source_list = $this->M_sources->getSources(null,true);

        $list = $this->M_summarys->getSummaryList($curr_page,$per_page,$filter);

        $data = array(
            'controller'=>$controller,
            'list'=>$list,
            'source_list'=>$source_list,
            'status_list'=>$config_arr['status']['name'],
            'status_color_list'=>$config_arr['status']['font_color']
        );

        Template::show('summary/index.php',$data);
    }

    //添加
    public function add(){
        global $controller;
        global $config_arr;

        $source_list = $this->M_sources->getSources(null,true);

        $data = array(
            'controller'=>$controller,
            'source_list'=>$source_list,
            'status_list'=>$config_arr['status']['name'],
        );

        Template::show('summary/add.php',$data);
    }

    //删除
    public function del(){
        $summary_id = $this->input()->get('summary_id');

        $ret = $this->M_summarys->delSummary($summary_id);

        if($ret){
            $data = array(
                "statusCode"=>"200",
                "message"=>"删除成功",
                "navTabId"=>"summary"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON(300,$data);
        }
    }

    //修改
    public function edit(){
        global $controller;
        global $config_arr;

        $summary_id = $this->input()->get('summary_id');

        $info = $this->M_summarys->get($summary_id);

        $source_list = $this->M_sources->getSources(null,true);

        $data = array(
            'controller'=>$controller,
            'info'=>$info,
            'source_list'=>$source_list,
            'status_list'=>$config_arr['status']['name'],
        );

        Template::show('summary/edit.php',$data);
    }

    //修改资讯为推荐状态
    public function recommend(){
        $summary_id = $this->input()->get('summary_id');

        if(!$summary_id){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误,推荐资讯摘要编号或者类型不存在！",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_summarys->recommendSummary($summary_id,5);

        if($ret){
            $data = array(
                "statusCode"=>"200",
                "message"=>"推荐资讯摘要推送到首页成功",
                "navTabId"=>"summary"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"推荐资讯摘要推送到首页失败",
            );
            echo Response::JSON(300,$data);
        }
    }

    //异步添加
    public function ajaxAdd(){

        $source_id = $this->input()->post('source_id');
        $title = $this->input()->post('title');
        $status = $this->input()->post('status');
        $summary = $this->input()->post('summary');
        $content = $this->input()->post('content');
		$url = $this->input()->post('url');
        $user_id = $this->session()->get('user_id',rand(13698,13714));

        if(!$source_id || !$title || !$status || !$summary || !$user_id || !$url){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误，请填写完入库表单！",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_summarys->addSummary($source_id, $title, $status, $summary, $content, $url, $user_id);
        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"添加成功",
                "navTabId"=>"summary",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"添加失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }

    //异步添加
    public function ajaxEdit(){

        $summary_id = $this->input()->post('summary_id');
        $source_id = $this->input()->post('source_id');
        $title = $this->input()->post('title');
        $status = $this->input()->post('status');
        $summary = $this->input()->post('summary');
        $content = $this->input()->post('content');
        $url = $this->input()->post('url');

        if(!$summary_id || !$source_id || !$title || !$status || !$summary || !$url){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误，请填写完入库表单！",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_summarys->editSummary($summary_id, $source_id, $title, $status, $summary, $content, $url);

        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"编辑成功",
                "navTabId"=>"summary",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"编辑失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }
}